<?php

class Admin_BankController extends Custom_Controller_Action
{
    public function indexAction()
    {
        $objBank = new Service_Model_DbTable_Bank();
        $page = $this->getRequest()->getParam('page');
		$objBank->setPaginator($this->Config->params->recordsPerPage,$page);
        $this->setSort($objBank);
        $banks = $objBank->getAll($this->searchKey);
        //echo '<pre>';print_r($customers);die;
        $this->view->banks = $banks;
		$this->view->paginator = $objBank->getPaginator();
		$this->view->page = $page;
        $this->_setTab(0);
    }

    public function createAction()
    {
        $form = new Admin_Form_Bank();
        $this->view->form = $form;

		if($this->getRequest()->isPost())
		{
			$objBank	= new Service_Model_DbTable_Bank();
			$formData = $this->getRequest()->getPost();

			if($form->isValid($formData))
			{
                $values = $form->getValues();
                $newId = $objBank->insert($values);

                if($newId)
                {
                    $this->addMessage("Bank created successfully");
                    $this->RedirectUrl("bank/create");
                }
                else
                {
                    $this->addMessage("Bank not created successfully", 'error');
                }
            }
        }

        $this->_setTab(1);
    }

    public function editAction()
    {
        $bid = $this->_getParam('id');

        if($bid)
        {
            $form = new Admin_Form_Bank();
            $id = Custom_General::decode($bid);
            $objBank = new Service_Model_DbTable_Bank();

            if($this->getRequest()->isPost())
            {
                $formData = $this->getRequest()->getPost();

                if($form->isValid($formData))
                {
                    $values = $form->getValues();
                    //if(empty($values['image'])) unset ($values['image']);
                    $res = $objBank->updateById($values, $id);

                    if($res)
                    {
                        $this->addMessage("Bank information updated successfully");
                        $this->RedirectUrl("admin/bank");
                    }
                    else
                    {
                        $this->addMessage("Bank information not updated successfully", 'error');
                    }
                }
            }

            $bank = $objBank->getOne($id);

            if($bank)
            {
                $form->populate($bank);
                $this->view->form = $form;
            }
        }

        $this->_setTab(2);
    }

    public function showAction()
	{
		$bid = $this->_getParam('id');

		if($bid)
		{
			$id = Custom_General::decode($bid);

			$objBank = new Service_Model_DbTable_Bank();
			$data = $objBank->getOne($id);
            $this->view->bank = $data;
			$this->view->bankLogoPath = $this->Config->params->bank->imagePath;
		}
		else
		{
			$this->view->bank = false;
		}

        $this->_setTab(3);
	}
    
    private function _setTab($activeIndx)
	{
		$subItems = array(__('Banks'),__('Create Bank'),__('Edit Bank'),__('Bank Details'));
		$subItemLinks = array('admin/bank','admin/bank/create','#','#');
		//$linkRoutes = array('bIndex','bCreate','bEdit','bShow');

		$menues = array('0','1');

		if(!in_array($activeIndx, $menues)) $menues[] = $activeIndx;

		$subMenues = array('items' => $subItems, 'links'=> $subItemLinks,
						   'enableMenues' => $menues, 'activeIndx' => $activeIndx);

		$this->view->subMenues = $subMenues;
	}
}
